<template>
  <el-dialog
    title="请绑定SSH登录凭据"
    :model-value="dialogVisible"
    :before-close="dialogClose"
    width="300px"
  >
    <el-form
      ref="formRef"
      :model="ruleForm"
      :rules="rules"
      inline
      label-position="left"
      label-width="155px"
      status-icon
      class="demo-ruleForm"
    >
      <el-form-item label="关联账号:" style="width: 100%" prop=" ">
        <el-select
          v-model="ruleForm.ssh_id"
          filterable
          clearable
          @click="getsshs"
          placeholder="选择账号"
        >
          <el-option
            v-for="item in sshOptions"
            :key="item.id"
            :label="`${item.name}-${item.ssh_port}`"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    </el-form>
  </el-dialog>
</template>
<script>
import { updateServer } from "@/api/asset/servers";
import { getsshs } from "@/api/asset/ssh";
export default {
  name: "addCredential",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    curId: {
      type: Number,
      default: null,
    },
  },

  data() {
    return {
      ruleForm: {
        ssh_id: "",
      },
      sshOptions: [],
      currentRow: "",
      rules: {
        ssh_id: [
          { required: true, message: "请选择关联登录账号", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    dialogVisible(v) {
      console.log("监听到子组件开启");
      if (v) {
        if (this.curId) {
          console.log("传入cuId:", this.curId);
          this.currentRow = this.curId;
        }
      }
    },
  },
  methods: {
    // 查询凭证
    getsshs() {
      getsshs().then((response) => {
        this.sshOptions = response.data.results;
        const passwd = [];
        for (let i in this.sshOptions) {
          passwd.push(
            this.sshOptions[i].name +
              "-" +
              this.sshOptions[i].authentication_type
          );
        }
        console.log("passwd 获取凭证:", passwd);
        console.log("sshOptions", this.sshOptions);
      });
    },
    dialogClose() {
      //   this.$refs.ruleForm.resetFields();
      console.log("子组件窗口数据已清理,通知父组件");
      this.$emit("close");
    },
    search() {
      this.dialogClose();
      this.$emit("search");
    },

    // 提交表单
    submitForm() {
      //   this.$refs[formName].validate((valid) => {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          if (this.curId) {
            updateServer(this.curId, this.ruleForm).then((res) => {
                res
              this.$message({
                message: "修改成功",
                type: "success",
              });

              this.search();
            //   this.dialogClose();

              console.log("this.curId", this.currentRow);
              console.log("this.ruleForm.ssh_id", this.ruleForm.ssh_id);

              const newpage = this.$router.resolve({
                path: "/webssh",
                name: "WSTerminal",
                params: {
                  hostid: this.currentRow,
                  sshinfo: this.ruleForm.ssh_id,
                },
              });
              const newWindow = window.open(newpage.href, "_blank");
              newWindow.hostid = this.currentRow;
              newWindow.sshinfo = this.ruleForm.ssh_id;
            });
          } else {
            return false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>